<template>
  <el-dialog v-dialogDrag :visible.sync="dialogVisible" :title="title" width="750px" @close="resetForm('ruleForm')">
    <button-group ref="buttonGroup" @submitForm="submitForm('ruleForm')" @cancel="dialogVisible = false" />
    <el-row>
      <el-form ref="ruleForm" :model="ruleForm" :rules="rules" label-width="auto">
        <el-row>
          <el-col :span="12">
            <el-form-item label="故障代码" prop="faultCode">
              <el-input v-model="ruleForm.faultCode" maxlength="20" show-word-limit />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="故障类型" prop="faultType">
              <el-input v-model="ruleForm.faultType" maxlength="20" show-word-limit />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="设备名称" prop="equipId">
              <el-select v-model="ruleForm.equipId" style="width: 100%">
                <el-option v-for="item in eqAllList" :key="item.id" :label="item.equipName" :value="item.id">
                  <div @click="eqInfo(item)">
                    <span>{{ item.equipName }}</span>
                    <b style="font-weight: normal">{{ item.equipCode ? '【' + item.equipCode + '】' : '' }}</b>
                    <b style="float: right; font-weight: normal">{{ item.type ? '【' + item.type + '】' : '' }}</b>
                  </div>
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="解决方案" prop="solution">
              <el-input v-model="ruleForm.solution" maxlength="20" show-word-limit />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="创建日期" prop="createDate">
              <el-date-picker v-model="ruleForm.createDate" :clearable="false" format="yyyy-MM-dd" value-format="yyyy-MM-dd" type="date" placeholder="选择日期时间" style="width: 100%" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="创建人" prop="userId">
              <el-select v-model="ruleForm.userId" style="width: 100%">
                <el-option v-for="item in userAllList" :key="item.id" :label="item.name" :value="item.id" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="ruleForm.remark" maxlength="200" type="textarea" rows="3" show-word-limit />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </el-row>
    <!-- <span slot="footer">
      <el-button type="info" @click="dialogVisible = false">取消</el-button>
      <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
    </span> -->
  </el-dialog>
</template>
<script>
export default {
  props: {
    userAllList: {
      type: Array,
      default: () => {
        return []
      }
    },
    eqAllList: {
      type: Array,
      default: () => {
        return []
      }
    }
  },
  data() {
    return {
      title: '',
      ruleForm: {
        equipId: '',
        faultType: '',
        faultCode: '',
        userId: '',
        createDate: '',
        solution: '',
        remark: ''
      },
      rules: {
        equipId: [{ required: true, message: '设备不能为空！', trigger: 'blur' }]
      },
      dialogVisible: false
    }
  },
  watch: {},
  mounted() {},
  methods: {
    /**
     * @description: 新增编辑
     * @param {*} row
     * @return {*}
     */
    async showDialog(row) {
      this.dialogVisible = true
      if (row && row.hasOwnProperty('id')) {
        this.title = '编辑'
        this.ruleForm = Object.assign({}, row)
      } else {
        this.title = '新增'
      }
    },
    eqInfo(row) {
      this.ruleForm.equipId = row.id
      this.ruleForm.equipCode = row.equipCode
    },
    /**
     * @description: 表单提交
     * @param {*} formName
     * @return {*}
     */
    submitForm(formName) {
      this.$refs[formName].validate(async valid => {
        if (valid) {
          const res = await this.$axios.post('/equip/knowledge/save', this.ruleForm)
          if (res?.data.code === 200) {
            this.$notify.success('提交成功！')
            this.dialogVisible = false
            this.$emit('getTableInfo')
          } else {
            this.$notify.error(res.data.msg)
          }
        }
      })
    },
    /**
     * @description: 重置表单
     * @param {*} formName
     * @return {*}
     */
    resetForm(formName) {
      this.$refs[formName].resetFields()
      this.ruleForm = {
        equipId: '',
        faultType: '',
        faultCode: '',
        userId: '',
        createDate: '',
        solution: '',
        remark: ''
      }
    }
  }
}
</script>
<style lang='scss' scoped>
</style>
